package org.lalib.controller.service;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.lalib.acquire.IControllerService;
import org.lalib.entity.object.ECategory;
import org.lalib.mediator.MCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service(value = "cCategoryService")
@Scope(value = "prototype")
public class CCategoryService implements IControllerService {

	private static final long serialVersionUID = 1L;

	@Autowired
	private MCategory mCategory;

	public void setmCategory(MCategory mCategory) {
		this.mCategory = mCategory;
	}

	public ECategory getRootCategory() {
		return mCategory.getRootCategory();
	}

	public List<ECategory> getAllCategoriesExceptForRootCategory() {
		return mCategory.getAllCategoriesExceptForRootCategory();
	}

	public void save(ECategory eCategory) {
		eCategory.setType(eCategory.getParentCategory().getType());
		mCategory.insert(eCategory);
	}

	public void update(ECategory eCategory, Long selectedCategoryId) {
		ECategory cat = mCategory.getEntityById(eCategory.getId());
		ECategory parentCategory = mCategory.getEntityById(selectedCategoryId);
		cat.setName(eCategory.getName());
		cat.setDescription(eCategory.getDescription());
		cat.setType(eCategory.getType());
		cat.setParentCategory(parentCategory);
		mCategory.update(cat);
	}

	public void delete(ECategory category) {
		category = mCategory.getCategoryWithChild(category.getId());
		List<ECategory> childCategories = category.getChildCategories();
		if (childCategories != null)
			for (ECategory cat : category.getChildCategories())
				delete(cat);
		category = mCategory.getEntityById(category.getId());
		mCategory.remove(category);
	}

	public List<ECategory> getAllParentCategoriesForGivenCategory(ECategory category) {
		List<ECategory> categoryList = new ArrayList<ECategory>();
		if (category != null && category.getParentCategory() != null) {
			categoryList.add(category.getParentCategory());
			categoryList.addAll(getAllParentCategoriesForGivenCategory(category.getParentCategory()));
			return categoryList;
		}
		return categoryList;
	}

	public List<SelectItem> getSelectItemListForCategory(List<ECategory> categoryList) {
		List<SelectItem> selectItemList = new ArrayList<>();
		for (ECategory category : categoryList)
			selectItemList.add(new SelectItem(category.getId(), category.getName()));
		return selectItemList;
	}

	public List<ECategory> getAllUserCategories() {
		return mCategory.getCategoriesByType("P");
	}

	public List<ECategory> getAllTableCategories() {
		return mCategory.getCategoriesByType("T");
	}

	public List<ECategory> getAllFoodCategories() {
		return mCategory.getCategoriesByType("F");
	}

	public List<ECategory> filterCategories(List<ECategory> userCategories, List<ECategory> allUserCategories) {
		List<ECategory> filteredCategories = new ArrayList<>();
		for (ECategory category : allUserCategories) {
			boolean check = true;
			for (ECategory userCategory : userCategories) {
				if (userCategory.getId().equals(category.getId())) {
					check = false;
					break;
				}
			}
			if (check) {
				filteredCategories.add(category);
			}
		}
		return filteredCategories;
	}

	public List<ECategory> filterUniqueCategories(List<ECategory> categories) {
		List<ECategory> eCategories = new ArrayList<>();

		for (ECategory eCategory : categories) {
			boolean check = true;
			for (ECategory category : eCategories)
				if (category.getId().equals(eCategory.getId())) {
					check = false;
					break;
				}
			if (check)
				eCategories.add(eCategory);
		}
		return eCategories;
	}

}